package com.cn.wanxi.servlet.back.user;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/user/manage")
public class UserManageServlet extends HttpServlet {
    IUserService userService = new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //用户删除
        resp.setContentType("text/html;charset=utf-8");
        String userid = req.getParameter("id");
        int id = 0;
        if (!"".equals(userid))
            id = Integer.parseInt(userid);
        int result = userService.deleteUser(id);
        resp.getWriter().print(result);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //用户修改
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String  userid = req.getParameter("id");
        int id = 0;
        if (userid != null)
            id = Integer.parseInt(userid);
        UserModel userModel = userService.getUser(id);
        userModel.setId(id);
        userModel.setUsername(req.getParameter("username"));
        userModel.setOldPassword(userModel.getPassword());
        userModel.setPassword(req.getParameter("password"));
        userModel.setIntro(req.getParameter("intro"));
        userModel.setBirth(req.getParameter("birth"));
        userModel.setPhone(req.getParameter("phone"));
        userModel.setSex(req.getParameter("sex"));
        userModel.setHobby(req.getParameter("hobby"));
        userModel.setUpdate_time(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = userService.updateUser(userModel);
        resp.getWriter().print(result);
    }
}
